package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import sun.swing.table.DefaultTableCellHeaderRenderer;

import javax.swing.*;
import javax.swing.event.ListDataListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;

//图书订购订单查询
public class FromOrderAddBook extends JInternalFrame {
    JTextField searchText = new JTextField(20);
    JButton searchBut = new JButton("入库");
    JTable bookTable;
    JScrollPane tableScrollPane;
    String searchCondition1;
    DefaultTableModel tableModel;

    public FromOrderAddBook() {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        getContentPane().setLayout(new BorderLayout());  //设置布局
        setBounds(10, 10, 1550, 600);
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setTitle("图书入库");

        // 建立表格
        Vector header1, columns1;
        header1 = Dao.orderInfoHead(false);
        columns1 = Dao.orderInfoColumns(true);
        tableModel = new DefaultTableModel(columns1, header1);
        //绘制表格
        bookTable = new JTable(tableModel);
        bookTable.setAutoCreateRowSorter(true);//设置点击表头可排序
        tableScrollPane = new JScrollPane(bookTable);
        bookTable.setRowHeight(25);//设置行高
        bookTable.setFont(new Font("宋体", Font.PLAIN, 20));//行字体大小
        bookTable.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 20));//表头字体大小
        FitTableColumns(bookTable);// 表格自适应列宽
        DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();//表格内容剧中
        thr.setHorizontalAlignment(JLabel.CENTER);
        bookTable.getTableHeader().setDefaultRenderer(thr);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);
        bookTable.setDefaultRenderer(Object.class, tcr);
        bookTable.addMouseListener(new tableAction());
        //把表格滚动面板加入主面板
        getContentPane().add(tableScrollPane, BorderLayout.NORTH);

        //添加ISBN和书名搜索栏面板
        /*JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new FlowLayout());
        //初始化下拉列表框模型类
        searchCondition[] searchConditions = new searchCondition[4];
        searchConditions[0] = new searchCondition("ISBN", "ISBN");
        searchConditions[1] = new searchCondition("书名", "bookname");
        searchConditions[2] = new searchCondition("作者", "writer");
        searchConditions[3] = new searchCondition("出版社", "publisher");
        conditionComBoxI myComBox = new conditionComBoxI();
        myComBox.setArrayList(searchConditions);
        JComboBox conditionComBox = new JComboBox(myComBox);
        conditionComBox.addItemListener(new conditionComBoxAction());
        conditionComBox.setFont(new Font("宋体", Font.PLAIN, 20));
        conditionComBox.setSelectedIndex(0);//设置默认选项

        centerPanel.add(conditionComBox);
*/
        JPanel centerPanel = new JPanel(new FlowLayout());
        // 搜索内容文本框
        searchText.setFont(new Font("宋体", Font.PLAIN, 20));
        centerPanel.add(searchText);

        // 搜索按钮
        searchBut.setFont(new Font("宋体", Font.PLAIN, 20));
        searchBut.addActionListener(new searchbooksBut());
        centerPanel.add(searchBut);

        getContentPane().add(centerPanel, BorderLayout.CENTER);
        validate();
    }

    // 列表框事件处理
    private class conditionComBoxAction implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            searchCondition item = (searchCondition) e.getItem();
            searchCondition1 = item.sqlHeader;
        }
    }

    // 列表元素类模型
    private class searchCondition {
        String showStr;
        String sqlHeader;

        searchCondition(String showStr, String sqlHeader) {
            this.showStr = showStr;
            this.sqlHeader = sqlHeader;
        }

        public String toString() {
            return showStr;
        }
    }

    // 搜索方式下拉列表框模型接口类
    private class conditionComBoxI implements ComboBoxModel {
        ArrayList<searchCondition> arrayList = new ArrayList<searchCondition>();
        Object curSelect;

        public void setArrayList(searchCondition[] arrayList) {
            for (searchCondition a : arrayList) {
                this.arrayList.add(a);
            }
        }

        @Override
        public void setSelectedItem(Object anItem) {
            this.curSelect = anItem;
        }

        @Override
        public Object getSelectedItem() {
            return curSelect;
        }

        @Override
        public int getSize() {
            return arrayList.size();
        }

        @Override
        public Object getElementAt(int index) {
            return arrayList.get(index);
        }

        @Override
        public void addListDataListener(ListDataListener l) {

        }

        @Override
        public void removeListDataListener(ListDataListener l) {

        }
    }

    private class searchbooksBut implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (searchText.getText().isEmpty()) {
                JOptionPane.showMessageDialog(FromOrderAddBook.this, "请选择或输入内容");
                return;
            } else {
                if (Dao.orderArrived(searchText.getText(),true)) {
                    JOptionPane.showMessageDialog(FromOrderAddBook.this, "已入库");
                }
            }
            Vector header1, columns1;
            header1 = Dao.orderInfoHead(false);
            columns1 = Dao.orderInfoColumns(true);
            tableModel.setDataVector(columns1, header1);
            FitTableColumns(bookTable);// 表格自适应列宽
            DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();//表格内容剧中
            thr.setHorizontalAlignment(JLabel.CENTER);
            bookTable.getTableHeader().setDefaultRenderer(thr);
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
            tcr.setHorizontalAlignment(JLabel.CENTER);
            bookTable.setDefaultRenderer(Object.class, tcr);
        }
    }

    // 实现对表格进行自适应
    public void FitTableColumns(JTable myTable) {
        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) myTable.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(myTable, column.getIdentifier()
                            , false, false, -1, col).getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,
                        myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width);
        }
    }

    private class tableAction implements MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {
            int rows = bookTable.getSelectedRow();
            int cols = bookTable.getSelectedColumn();
            if (cols == 0) {
                searchText.setText((String) bookTable.getValueAt(rows,cols));
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }
}


